<template>
  <div class="countbox">
    <button class="coun-lift" @click="hesubb">-</button>
    <input
      :value="value"
      class="coun-cent"
      type="text"
      @change="handleChange"
    />
    <button class="coun-right" @click="headeladd">+</button>
  </div>
</template>
<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  components: {},
  data () {
    return {}
  },
  computed: {},
  // 方法集合",
  methods: {
    hesubb () {
      if (this.value <= 1) {
        return
      }
      this.$emit('input', this.value - 1)
    },
    headeladd () {
      this.$emit('input', this.value + 1)
    },
    handleChange (e) {
      const num = +e.target.value // 转数字处理
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created () {}
}
</script>
<style lang="less">
.countbox {
  width: 120px;
  display: flex;
  justify-content: space-between;

  .coun-lift,
  .coun-right {
    width: 30px;
    height: 30px;
    outline: none;
    border: none;
    background-color: rgba(192, 192, 192, 0.3);
  }

  .coun-cent {
    width: 40px;
    height: 30px;
    background-color: rgba(192, 192, 192, 0.3);
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>
